package uit.edu.vn;

import java.util.ArrayList;
import java.util.List;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class SMSListAdapter extends BaseAdapter {
	
	private Context context;
	private List<mySMS> smsList;

	private ArrayList<Boolean> itemSMSChecked = new ArrayList<Boolean>();
	private LayoutInflater mInflater;
	public SMSListAdapter(Context context , List<mySMS> smsList) 
	{
		// TODO Auto-generated constructor stub
		this.context=context;
		this.smsList=smsList;

		mInflater= LayoutInflater.from(context);
		// thiêt lap mac dinh itemcheck là false
		for (int i = 0; i < this.getCount(); i++) {
		       itemSMSChecked.add(i, false); // initializes all items value with false
		   }
	}
	@Override
	public Object getItem(int position) 
	{
		// TODO Auto-generated method stub
		return smsList.get(position);
	}
	@Override
	public int getCount() 
	{
		// TODO Auto-generated method stub
		return smsList.size();
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		mySMS entry = smsList.get(position);
		if(convertView==null)
		{
			convertView=mInflater.inflate(R.layout.row_layout, null);
		}
		
		TextView txt_smsName= (TextView)convertView.findViewById(R.id.txt_SMSName);
		TextView txt_smsBody=(TextView)convertView.findViewById(R.id.txt_SMSBody);
		txt_smsName.setText(entry.getSMSNumber());
		txt_smsBody.setText(entry.getSMSBody());
		 //xy lu check box 
		 CheckBox cbox= (CheckBox)convertView.findViewById(R.id.chkSelectSMS);
		 cbox.setOnClickListener(new OnClickListener() {

		        public void onClick(View v) {

		            CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelectSMS);

		            if (cb.isChecked()) {
		                itemSMSChecked.set(position, true);
	
		            } else if (!cb.isChecked()) {
		                itemSMSChecked.set(position, false);
		
		            }
		        }
		    });

		cbox.setChecked(itemSMSChecked.get(position));
		
		
		return convertView;
	}
	public ArrayList<Boolean> getCheckList()
	{
		return itemSMSChecked;
	}
	public ArrayList<Boolean> setCheckList(ArrayList<Boolean> itemCheck)
	{
		itemSMSChecked=itemCheck;
		return itemSMSChecked;
	}
}
